using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class IAPManager
{
	public enum ProductType
	{
		None = 0,
		LivesPack1 = 1,
		LivesPack2 = 2,
		LivesPack3 = 3,
		WildcardsPack1 = 4,
		WildcardsPack2 = 5,
		WildcardsPack3 = 6,
		Unlock = 7,
		RemoveAds = 8
	}

	public delegate void ProductListReceivedEventHandler();

	public delegate void PurchaseSuccessEventHandler(ProductType purchaseType);

	public delegate void PurchaseFailedEventHandler();

	public delegate void RestorePurchaseFinishedEventHandler(bool success);

	private static Dictionary<ProductType, string> defaultProductPrices = new Dictionary<ProductType, string>
	{
		{
			ProductType.LivesPack1,
			"$0.99"
		},
		{
			ProductType.LivesPack2,
			"$1.99"
		},
		{
			ProductType.LivesPack3,
			"$3.99"
		},
		{
			ProductType.WildcardsPack1,
			"$0.99"
		},
		{
			ProductType.WildcardsPack2,
			"$1.99"
		},
		{
			ProductType.WildcardsPack3,
			"$3.99"
		},
		{
			ProductType.Unlock,
			"$7.99"
		},
		{
			ProductType.RemoveAds,
			"$0.99"
		}
	};

	public static ProductType[] purchasesThatRemoveAds = new ProductType[1] { ProductType.Unlock };

	private bool requestProductsAtInit = true;

	private Dictionary<ProductType, int> quantitiesForProducts = new Dictionary<ProductType, int>
	{
		{
			ProductType.LivesPack1,
			100
		},
		{
			ProductType.LivesPack2,
			250
		},
		{
			ProductType.LivesPack3,
			600
		},
		{
			ProductType.WildcardsPack1,
			5
		},
		{
			ProductType.WildcardsPack2,
			12
		},
		{
			ProductType.WildcardsPack3,
			30
		},
		{
			ProductType.Unlock,
			0
		},
		{
			ProductType.RemoveAds,
			0
		}
	};

	private string googleIABkey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0G1dKQVkuidYOiF6a4qsDWKrJyUQV95DJmxqmNjQC1DsTdFFEt2h3ZEwQ1KkXZIxhSchEEDIHtTmkQPl6cFave/I2/GSW0YDMzxcBdWHR79C/Ug6O/UAu+pWntzhsUeCG5ZlKrGxHtZZDgtUxvvCtGmhhRY2ngIYr5oBUXp3ZGYgUOr5TWpgFxyckdrPpIZk9v/gLuGxL88VUZBTgPH02ma9YAT24TzKOXkWUP/FRB2BfKbiTjZvivOoHPa6Zw0vuXSfup0GuQ93jxhWVG+BTDpMWm4BCk3Vr4wh7muZo1/jWwL0cd0UAzrByZ3L6KscmKERziH8gtJLqy+RpwmkGwIDAQAB";

	private bool billingSupported;

	private List<GoogleSkuInfo> products;

	private Dictionary<ProductType, string> productIds = new Dictionary<ProductType, string>
	{
		{
			ProductType.LivesPack1,
			"gyrosphere_spheres_pack_1"
		},
		{
			ProductType.LivesPack2,
			"gyrosphere_spheres_pack_2"
		},
		{
			ProductType.LivesPack3,
			"gyrosphere_spheres_pack_3"
		},
		{
			ProductType.WildcardsPack1,
			"gyrosphere_wildcards_pack_1"
		},
		{
			ProductType.WildcardsPack2,
			"gyrosphere_wildcards_pack_2"
		},
		{
			ProductType.WildcardsPack3,
			"gyrosphere_wildcards_pack_3"
		},
		{
			ProductType.Unlock,
			"gyrosphere_unlock_all_levels"
		},
		{
			ProductType.RemoveAds,
			"gyrosphere_remove_ads"
		}
	};

	[method: MethodImpl(32)]
	public event ProductListReceivedEventHandler ProductListReceivedEvent;

	[method: MethodImpl(32)]
	public event PurchaseSuccessEventHandler PurchaseSuccessEvent;

	[method: MethodImpl(32)]
	public event PurchaseFailedEventHandler PurchaseFailedEvent;

	[method: MethodImpl(32)]
	public event RestorePurchaseFinishedEventHandler RestorePurchaseFinishedEvent;

	public IAPManager()
	{
		Debug.Log("IAP Manager init");
		//GoogleIAB.init(googleIABkey);
		//GoogleIAB.enableLogging(true);
		//GoogleIABManager.billingSupportedEvent += BillingSupportedEvent;
		//GoogleIABManager.billingNotSupportedEvent += BillingNotSupportedEvent;
		//GoogleIABManager.queryInventorySucceededEvent += QueryInventorySucceededEvent;
		//GoogleIABManager.queryInventoryFailedEvent += QueryInventoryFailedEvent;
		//GoogleIABManager.purchaseSucceededEvent += PurchaseSuccess;
		//GoogleIABManager.purchaseFailedEvent += PurchaseFailed;
		//GoogleIABManager.consumePurchaseSucceededEvent += ConsumeSuccess;
		//GoogleIABManager.consumePurchaseFailedEvent += ConsumeFailed;
	}

	public static bool DoesPurchaseRemoveAds(ProductType pType)
	{
		for (int i = 0; i < purchasesThatRemoveAds.Length; i++)
		{
			if (purchasesThatRemoveAds[i] == pType)
			{
				return true;
			}
		}
		return false;
	}

	public static string GetDefaultFormattedPrice(ProductType pType)
	{
		if (defaultProductPrices.ContainsKey(pType))
		{
			return defaultProductPrices[pType];
		}
		return string.Empty;
	}

	public int GetQuantityForProductId(ProductType pType)
	{
		if (!quantitiesForProducts.ContainsKey(pType))
		{
			return -1;
		}
		return quantitiesForProducts[pType];
	}

	private void RequestProducts()
	{
		if (products != null)
		{
			return;
		}
		List<string> list = new List<string>();
		foreach (KeyValuePair<ProductType, string> productId in productIds)
		{
			if (productId.Value != string.Empty && !list.Contains(productId.Value))
			{
				list.Add(productId.Value);
			}
		}
		Debug.Log("Requesting products ! pIds.Count = " + list.Count);
		//GoogleIAB.queryInventory(list.ToArray());
	}

	public ProductType GetProductTypeForId(string productId)
	{
		foreach (KeyValuePair<ProductType, string> productId2 in productIds)
		{
			if (productId2.Value == productId)
			{
				return productId2.Key;
			}
		}
		return ProductType.None;
	}

	public bool CanPurchase(ProductType type)
	{
		if (!billingSupported)
		{
			Debug.Log("CanPurchase->Cannot make purchases!");
			return false;
		}
		if (products == null)
		{
			Debug.Log("CanPurchase -> Request products first!");
			return false;
		}
		for (int i = 0; i < products.Count; i++)
		{
			if (products[i].productId == productIds[type])
			{
				return true;
			}
		}
		return false;
	}

	public void Purchase(ProductType type)
	{
		if (!CanPurchase(type))
		{
			return;
		}
		for (int i = 0; i < products.Count; i++)
		{
			if (products[i].productId == productIds[type])
			{
				//GoogleIAB.purchaseProduct(products[i].productId);
				return;
			}
		}
		Debug.Log("Trying to buy a product that is not available!");
	}

	public void RestorePurchases()
	{
		if (this.RestorePurchaseFinishedEvent != null)
		{
			this.RestorePurchaseFinishedEvent(true);
		}
	}

	private void BillingSupportedEvent()
	{
		Debug.Log("Billing supported!");
		billingSupported = true;
		if (requestProductsAtInit)
		{
			RequestProducts();
		}
	}

	private void BillingNotSupportedEvent(string error)
	{
		billingSupported = false;
		Debug.Log("Billing NOT supported! " + error);
	}

	private void QueryInventorySucceededEvent(List<GooglePurchase> purchases, List<GoogleSkuInfo> skuInfo)
	{
		Debug.Log("Query inventory success " + skuInfo.Count);
		products = skuInfo;
		for (int i = 0; i < purchases.Count; i++)
		{
			ProductType productTypeForId = GetProductTypeForId(purchases[i].productId);
			if (productTypeForId == ProductType.None)
			{
				Debug.Log("ERROR :  invalid product id! " + purchases[i].productId);
				continue;
			}
			switch (productTypeForId)
			{
			case ProductType.RemoveAds:
				if (!Economy.adsDisabled)
				{
					Debug.Log("Restoring ads disabled purchase!");
					if (this.PurchaseSuccessEvent != null)
					{
						this.PurchaseSuccessEvent(productTypeForId);
					}
				}
				break;
			case ProductType.Unlock:
				if (!Economy.arenasPurchased)
				{
					Debug.Log("Restoring arena unlock purchase!");
					if (this.PurchaseSuccessEvent != null)
					{
						this.PurchaseSuccessEvent(productTypeForId);
					}
				}
				break;
			default:
				//GoogleIAB.consumeProduct(purchases[i].productId);
				break;
			}
		}
		if (this.ProductListReceivedEvent != null)
		{
			this.ProductListReceivedEvent();
		}
	}

	private void QueryInventoryFailedEvent(string error)
	{
		products = null;
		Debug.Log("Query inventory failed " + error);
	}

	private void PurchaseSuccess(GooglePurchase purchase)
	{
		Debug.Log("PurchaseSuccess " + purchase.productId);
		ProductType productTypeForId = GetProductTypeForId(purchase.productId);
		switch (productTypeForId)
		{
		case ProductType.None:
			break;
		default:
			//GoogleIAB.consumeProduct(purchase.productId);
			break;
		case ProductType.Unlock:
		case ProductType.RemoveAds:
			if (this.PurchaseSuccessEvent != null)
			{
				this.PurchaseSuccessEvent(productTypeForId);
			}
			break;
		}
	}

	private void PurchaseFailed(string error, int errorCode)
	{
		Debug.Log("Failed purchasing product : " + error);
		if (this.PurchaseFailedEvent != null)
		{
			this.PurchaseFailedEvent();
		}
	}

	private void ConsumeSuccess(GooglePurchase purchase)
	{
		Debug.Log("ConsumeSuccess " + purchase.productId);
		ProductType productTypeForId = GetProductTypeForId(purchase.productId);
		if (productTypeForId == ProductType.None)
		{
			Debug.Log("Failed consuming product , invalid id " + purchase.productId);
			if (this.PurchaseFailedEvent != null)
			{
				this.PurchaseFailedEvent();
			}
		}
		else if (this.PurchaseSuccessEvent != null)
		{
			this.PurchaseSuccessEvent(productTypeForId);
		}
	}

	private void ConsumeFailed(string error)
	{
		Debug.Log("Failed consuming product : " + error);
		if (this.PurchaseFailedEvent != null)
		{
			this.PurchaseFailedEvent();
		}
	}

	public string GetFormattedPrice(ProductType pType)
	{
		if (products == null || products.Count == 0)
		{
			Debug.Log("Getting price : no products found!");
			return string.Empty;
		}
		for (int i = 0; i < products.Count; i++)
		{
			GoogleSkuInfo googleSkuInfo = products[i];
			if (productIds.ContainsKey(pType) && productIds[pType] == googleSkuInfo.productId)
			{
				return googleSkuInfo.price;
			}
		}
		return string.Empty;
	}
}
